package com.dwxt.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期时间工具类
 *
 * @author Administrator
 */
public class DateUtils {

    public static final SimpleDateFormat TIME_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
    //public static final Date now = new Date();

    /**
     * 格式化日期字符串
     * 列如  2020-5  格式化为  2020-05
     *
     * @return
     */
    public static String formatDateStr(String date) throws ParseException {
        final SimpleDateFormat YEAR_MONTH_FORMAT = new SimpleDateFormat("yyyy-MM");
        return YEAR_MONTH_FORMAT.format(YEAR_MONTH_FORMAT.parse(date));
    }

    /**
     * 判断一个时间是否在另一个时间之前
     *
     * @param time1 第一个时间
     * @param time2 第二个时间
     * @return 判断结果
     */
    public static boolean before(String time1, String time2) {
        try {
            Date dateTime1 = TIME_FORMAT.parse(time1);
            Date dateTime2 = TIME_FORMAT.parse(time2);

            if (dateTime1.before(dateTime2)) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 判断一个时间是否在另一个时间之后
     *
     * @param time1 第一个时间
     * @param time2 第二个时间
     * @return 判断结果
     */
    public static boolean after(String time1, String time2) {
        try {
            Date dateTime1 = TIME_FORMAT.parse(time1);
            Date dateTime2 = TIME_FORMAT.parse(time2);

            if (dateTime1.after(dateTime2)) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 计算时间差值（单位为秒）
     *
     * @param time1 时间1
     * @param time2 时间2
     * @return 差值
     */
    public static int minus(String time1, String time2) {
        try {
            Date datetime1 = TIME_FORMAT.parse(time1);
            Date datetime2 = TIME_FORMAT.parse(time2);

            long millisecond = datetime1.getTime() - datetime2.getTime();

            return Integer.valueOf(String.valueOf(millisecond / 1000));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 获取年月日和小时
     *
     * @param datetime 时间（yyyy-MM-dd HH:mm:ss）
     * @return 结果
     */
    public static String getDateHour(String datetime) {
        String date = datetime.split(" ")[0];
        String hourMinuteSecond = datetime.split(" ")[1];
        String hour = hourMinuteSecond.split(":")[0];
        return date + "_" + hour;
    }

    /**
     * 获取当天日期（yyyy-MM-dd）
     *
     * @return 当天日期
     */
    public static String getTodayDate() {
        return DATE_FORMAT.format(new Date());
    }

    /**
     * 获取昨天的日期（yyyy-MM-dd）
     *
     * @return 昨天的日期
     */
    public static String getYesterdayDate() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.DAY_OF_YEAR, -1);

        Date date = cal.getTime();

        return DATE_FORMAT.format(date);
    }

    /**
     * 格式化日期（yyyy-MM-dd）
     *
     * @param date Date对象
     * @return 格式化后的日期
     */
    public static String formatDate(Date date) {
        return DATE_FORMAT.format(date);
    }

    public static Date formatDate(String date) throws ParseException {
        return DATE_FORMAT.parse(date);
    }

    /**
     * 格式化时间（yyyy-MM-dd HH:mm:ss）
     *
     * @param date Date对象
     * @return 格式化后的时间
     */
    public static String formatTime(Date date) {
        return TIME_FORMAT.format(date);
    }

    public static Date formatTime(String date) {
        try {
            return TIME_FORMAT.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static int getInteger(Date date, int dateType) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(dateType);
    }

    public static int getYear(Date date) {
        return getInteger(date == null ? new Date() : date, Calendar.YEAR);
    }

    public static int getMonth(Date date) {
        return getInteger(date == null ? new Date() : date, Calendar.MONTH) + 1;
    }

    public static int getDay(Date date) {
        return getInteger(date == null ? new Date() : date, Calendar.DAY_OF_MONTH);
    }


    /**
     * 是否超过一天
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public static boolean overOneDay(Date startTime, Date endTime) {
        long between = endTime.getTime() - startTime.getTime();
        if (between > (24 * 3600000)) {
            return true;
        }
        return false;
    }

    public static boolean isSameDate(Date before, Date after) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(before);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(after);


        boolean isSameYear = cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR);
        if (isSameYear) {
            boolean isSameMonth = cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH);
            if (isSameMonth) {
                boolean isSameDate = cal1.get(Calendar.DAY_OF_MONTH) == cal2.get(Calendar.DAY_OF_MONTH);
                return isSameDate;
            }
        }
        return false;
    }

    /**
     * 将  11:11:11 设置为  当前年-当前月-当前天 11：11：11
     *
     * @return
     */
    public static Date stringTime2DateTime(String time) {
        Calendar instance = Calendar.getInstance();
        String[] split = time.split(":");
        instance.set(Calendar.HOUR_OF_DAY, Integer.parseInt(split[0]));
        instance.set(Calendar.MINUTE, Integer.parseInt(split[1]));
        instance.set(Calendar.SECOND, Integer.parseInt(split[2]));
        return instance.getTime();

    }

    public static boolean isContinueDay(Date start, Date end) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(start);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(end);


        boolean isSameYear = cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR);
        if (isSameYear) {
            boolean isSameMonth = cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH);
            if (isSameMonth) {
                boolean isContinueDay = cal1.get(Calendar.DAY_OF_MONTH) == cal2.get(Calendar.DAY_OF_MONTH) - 1;
                return isContinueDay;
            }
        }
        return false;
    }



    /**
     * 获取过去第几天的日期
     *
     * @param date
     * @param past
     * @return
     */
    public static String getPastDate(Date date, int past)   {
        if(date==null){
            date=new Date();
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past + 1);
        Date today = calendar.getTime();
        String result = format.format(today);
        return result;
    }

    /**
     * 获取未来 第 past 天的日期
     *
     *
     * @param parse
     * @param past
     * @return
     */
    public static String getFetureDate(Date date, int past) {
        if(date==null){
            date=new Date();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + past);
        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String result = format.format(today);
        return result;
    }
}